package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.Constant;
import com.srwl.mytx.adapter.LocalSearchAdapter;
import com.srwl.mytx.dao.DBManager;
import com.srwl.mytx.im.conversation.Conversation;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.greendao.MessageDao;
import com.srwl.mytx.greendao.UserDao;

import java.util.ArrayList;
import java.util.List;

/**
 * 本地搜索结果展示
 */
public class LocalSearchView extends RecyclerView implements LocalSearchAdapter.InteractionListener {

    private static final String TAG = "LocalSearchResultView";
    private Context context;
    private List<Conversation> conversationList = new ArrayList<>();
    private LocalSearchAdapter localSearchAdapter;


    public LocalSearchView(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public LocalSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public LocalSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    private void init() {

        setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        localSearchAdapter = new LocalSearchAdapter(context, conversationList, this);
        setAdapter(localSearchAdapter);
//        ItemTouchHelper touchHelper = new ItemTouchHelper(new MyItemTouchHelperCallBack());
//        touchHelper.attachToRecyclerView(this);

    }


    @Override
    public void onLongClick(int position, final Conversation conversation) {

//        ConfirmDialog confirmDialog = new ConfirmDialog(context, "提醒", "删除该会话？", true);
//        confirmDialog.show();
//        confirmDialog.setConfirmListener(new ConfirmDialog.ConfirmListener() {
//            @Override
//            public void confirm() {
//                conversationService.deleteConversation(conversation);
//
//            }
//        });

    }

    public void search(String content) {
        conversationList.clear();
        //查询本地联系人
        List<User> users = DBManager.getInstance().getDaoSession().getUserDao().queryBuilder().where(UserDao.Properties.Username.like("%" + content + "%")).orderDesc(UserDao.Properties.Created).list();

        for (User user : users) {
            Conversation conversation = getConversation(user.getuId());
            user.setHighlightKeyWord(content);
            conversation.setToUser(user);
        }
        //查询本地消息
        List<Message> messages = DBManager.getInstance().getDaoSession().getMessageDao().queryBuilder().where(MessageDao.Properties.SubType.eq(Constant.IM_TYPE_TEXT), MessageDao.Properties.Content.like("%" + content + "%")).orderDesc(MessageDao.Properties.Created).list();

        for (Message message : messages) {
            message.setHighlightKeyWord(content);

            Conversation conversation = getConversation(message.getcId());
            conversation.addMessage(message);
        }

        localSearchAdapter.notifyDataSetChanged();
    }


    synchronized public Conversation getConversation(String toUserId) {
        for (Conversation conversation : conversationList) {
            if (conversation.getCId().equals(toUserId)) {
                conversation.setToUserId(toUserId);
                return conversation;
            }
        }

        Conversation conversation = new Conversation(toUserId,Constant.CONVERSATION_TYPE_C2C);
        conversationList.add(conversation);
        return conversation;
    }
//    @Override
//    public void onConversationAdd(Conversation conversation) {
//        conversationList.add(conversation);
//        if (conversationListAdapter.isShowFilterData()) {
//            conversationListAdapter.filter();
//        } else {
//            conversationListAdapter.notifyItemInserted(conversationList.size() - 1);
//        }
//    }
//
//    @Override
//    public void onConversationRemove(Conversation conversation) {
//        int index = conversationList.indexOf(conversation);
//        conversationList.remove(index);
//
//        if (conversationListAdapter.isShowFilterData()) {
//            conversationListAdapter.notifyItemRemovedOnShowFilterData(conversation);
//        } else {
//            conversationListAdapter.notifyItemRemoved(index);
//        }
//
//    }
//
//    @Override
//    public void onConversationChange(Conversation conversation) {
//        int index = conversationList.indexOf(conversation);
//        if (conversationListAdapter.isShowFilterData()) {
//            conversationListAdapter.notifyItemChangedOnShowFilterData(conversation);
//        } else {
//            conversationListAdapter.notifyItemChanged(index);
//        }
//    }

//    public class MyItemTouchHelperCallBack extends ItemTouchHelper.Callback {
//        @Override
//        public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) {
//            int dragFlags = ItemTouchHelper.ACTION_STATE_IDLE;
//            int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
//            return makeMovementFlags(dragFlags, swipeFlags);
//        }
//
//        @Override
//        public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) {
//            return false;
//        }
//
//        @Override
//        public void onSwiped(ViewHolder viewHolder, int direction) {
//            Log.d(TAG, "onSwiped: 滑动删除，，，，");
//            int position = viewHolder.getAdapterPosition();
//            final Conversation conversation = conversationList.get(position);
//            conversationService.deleteConversation(conversation);
//
//
//        }
//    }


}
